package com.gec.hrm.servlet;

import com.gec.hrm.dao.impl.DeptDao;
import com.gec.hrm.dao.impl.DeptDaoImpl;
import com.gec.hrm.dao.impl.UserDao;
import com.gec.hrm.dao.impl.UserDaoImpl;
import com.gec.hrm.pojo.Dept;
import com.gec.hrm.pojo.Job;
import com.gec.hrm.pojo.R;
import com.gec.hrm.pojo.User;
import com.google.gson.Gson;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet(urlPatterns = {"/deptList.action","/addDept.action","/checkDeptName.action","/updDept.action","/delDepts.action","/delDept.action","/getAllDept.action"})
public class DeptServlet extends HttpServlet {
    public DeptDao deptDao = new DeptDaoImpl();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String url = request.getRequestURI();
        System.out.println(url);
        String action = url.substring(url.lastIndexOf("/") + 1);
        System.out.println(action);

        if (action.equals("deptList.action")) {
            deptList(request, response);
        } else if (action.equals("checkDeptName.action")) {
            cheackDeptName(request, response);
        } else if (action.equals("addDept.action")) {
            addDept(request, response);
        } else if (action.equals("updDept.action")) {
            updDept(request, response);
        } else if (action.equals("delDepts.action")) {
            delDepts(request, response);
        } else if (action.equals("delDept.action")) {
            delDept(request, response);
        }else if (action.equals("getAllDept.action")) {
            getAllDept(request, response);
        }
    }

    private void getAllDept(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setCharacterEncoding(("UTF-8"));
        PrintWriter out =response.getWriter();
        List<Dept> allJob = deptDao.getAllDept();
        out.print(new Gson().toJson(allJob));
    }

    private void delDept(HttpServletRequest request, HttpServletResponse response) {
        int id = Integer.parseInt(request.getParameter("id"));

        HttpSession session = request.getSession();

        try {
            PrintWriter out = response.getWriter();
            int i = deptDao.delDept(id);
            if (i > 0) {
                out.print(1);
            } else if(i==-1) {
                out.print(-1);
            }else{
                out.print(0);
            }
        } catch (IOException ioException) {
            ioException.printStackTrace();
        }

    }

    private void delDepts(HttpServletRequest request, HttpServletResponse response) {
        String ids = request.getParameter("ids");
        var id = ids.split(",");

        HttpSession session = request.getSession();
        try {
            PrintWriter out = response.getWriter();
            R r = new R();
            for (var ix : id) {
                int i = deptDao.delDept(Integer.parseInt(ix));
                if (i <0 ) {
                    out.print(0);
                    return;
                }
            }
            out.print(1);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void updDept(HttpServletRequest request, HttpServletResponse response) {
        String name = request.getParameter("name");
        String remark = request.getParameter("remark");
        int id = Integer.parseInt(request.getParameter("id"));

        Dept dept = new Dept(id, name, remark);
        int i = deptDao.updDept(dept);

        try {
            PrintWriter out = response.getWriter();
            if (i > 0) {
                out.print(1);
            } else {
                out.print(0);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    private void addDept(HttpServletRequest request, HttpServletResponse response) {
        String name = request.getParameter("name");
        String remark = request.getParameter("remark");
        Dept dept = new Dept(name, remark);
        int i = deptDao.addDept(dept);

        try {
            PrintWriter out = response.getWriter();
            if (i > 0) {
                out.print(1);
            } else {
                out.print(0);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    private void cheackDeptName(HttpServletRequest request, HttpServletResponse response) {
        // 取出用户输入的条件
        String name = request.getParameter("name");
        boolean flag = deptDao.checkDeptName(name);
        PrintWriter out = null;
        try {
            out = response.getWriter();
            if (flag) {
                out.print(0);
            } else {
                out.print(1);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    private void deptList(HttpServletRequest request, HttpServletResponse response) {
        //处理乱码问题
        response.setCharacterEncoding("UTF-8");
        //通过响应的response获取打印输出流
        PrintWriter out = null;
        try {
            out = response.getWriter();
            // 取出用户输入的条件
            String name = request.getParameter("name");
            //调用查询方法
            int page = Integer.parseInt(request.getParameter("page"));
            int limit = Integer.parseInt(request.getParameter("limit"));
            List<Dept> users = deptDao.deptList(name, page, limit);
            int count = deptDao.count();
            //lauyi框架返回四个参数
            R r = new R();
            r.put("msg", "查询成功");
            r.put("data", users);
            r.put("count", count);
            r.put("code", 0);
            //将对象返回到页面上，响应数据--respons--打印输出流
            out.print(new Gson().toJson(r));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("dopost");
        doGet(request, response);
    }
}
